<?php
/**
 * 2017年6月6日11:00:31
 * PDO SAFE
 */
include 'common.php';
$username = $_COOKIE['username'];
$token = $_COOKIE['token'];
$uuid = $_REQUEST['uuid'];

$dbc = pdo_root_connect();

$errorcode = 0;

$ip = $_SERVER['REMOTE_ADDR'];
try {
	$stmt = $dbc->prepare('select * from userinfo where username=:username and token=:token and status=0');
	$stmt->bindParam(':username', $username);
	$stmt->bindParam(':token', $token);
	$stmt->execute();
	if($stmt->rowCount() > 0)
	{
		$new_token = pdo_createtoken($username);
		//"update userinfo set uuid='$UUID',llip='$ip',changetime='$time',token='$qid' where username='$username' and token='$token'";//old
		$stmt2 = $dbc->prepare('update userinfo set uuid=:uuid,llip=:llip,changetime=unix_timestamp(now()),token=:new_token 
	where username=:username and token=:token and status=0');
		$stmt2->bindParam(':uuid', $uuid, PDO::PARAM_STR);
		$stmt2->bindParam(':llip', $ip, PDO::PARAM_STR);
		$stmt2->bindParam(':new_token', $new_token, PDO::PARAM_STR);
		$stmt2->bindParam(':username', $username, PDO::PARAM_STR);
		$stmt2->bindParam(':token', $token, PDO::PARAM_STR);
		$stmt2->execute();
		
		$errorcode = 2;
		if($stmt2->rowCount() > 0)
		{
			//更新记录成功 下发token
			setcookie('username', $username, time() + 60*60*24*5);
			setcookie('token', $new_token, time() + 60*60*24*5);
			$errorcode = 1;
		}
		
	}else{
		$errorcode = 3;
	}
	}catch (PDOException $e){
		$errorcode = 4;
		$msg = $e->getMessage();
	}
echo json_encode(array('errorcode'=>$errorcode,'username'=>$username, 'msg' => $msg, 'token'=>$qid));

?>